﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using System.Web.Security;

namespace BitPortal.Admin.Media.Media.Module
{
    public partial class MediaAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaInsert");
                BitPortal.Models.Media m = new Models.Media()
                {
                    Title = txtTitle.Text,
                    Description = txtDescription.Text,
                    Tags = txtTag.Text,
                    IsEnabled = ckEnabled.Checked
                };
                m.Logs.DatetimeCreated = m.Logs.DatetimeEdited = DateTime.Now;
                m.Logs.CreatingUser = m.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());
                switch (TabPanel1.ActiveTabIndex)
                {
                    case 0:
                        m.Type = "Image";
                        m.ThumbImage = m.MediaURL = imageUrl.TextFieldURL.Text;
                        break;
                    case 1:
                        m.Type = "Video";
                        m.MediaURL = videoUrl.TextFieldURL.Text;
                        m.ThumbImage = thumbnailUrl.TextFieldURL.Text;
                        break;
                    case 2:
                        m.Type = "Flash";
                        m.MediaURL = flashUrl.TextFieldURL.Text;
                        m.ThumbImage = "/Images/flash_player.png";
                        break;
                    case 3:
                        m.Type = "Audio";
                        m.MediaURL = audioUrl.TextFieldURL.Text;
                        m.ThumbImage = "/Images/audio_x_monkey.png";

                        break;
                }
                if (BitPortal.Business.MediaBLL.Insert(m) > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm media thành công!");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                long mediaID = Convert.ToInt64(txtID.Text);
                BitPortal.Models.Media m = bitEnt.Medias.SingleOrDefault(ent => ent.MediaID == mediaID);

                if (m != null)
                {
                    m.Title = txtTitle.Text;
                    m.Description = txtDescription.Text;
                    m.Tags = txtTag.Text;
                    m.IsEnabled = ckEnabled.Checked;
                    m.Logs.DatetimeEdited = DateTime.Now;
                    m.Logs.EditingUser = (Membership.GetUser() == null) ? null : (Guid?)new Guid(Membership.GetUser().ProviderUserKey.ToString());

                    switch (TabPanel1.ActiveTabIndex)
                    {
                        case 0:
                            m.Type = "Image";
                            m.ThumbImage = m.MediaURL = imageUrl.TextFieldURL.Text;
                            break;
                        case 1:
                            m.Type = "Video";
                            m.MediaURL = videoUrl.TextFieldURL.Text;
                            m.ThumbImage = thumbnailUrl.TextFieldURL.Text;
                            break;
                        case 2:
                            m.Type = "Flash";
                            m.MediaURL = flashUrl.TextFieldURL.Text;
                            m.ThumbImage = "/Images/flash_player.png";
                            break;
                        case 3:
                            m.Type = "Audio";
                            m.MediaURL = audioUrl.TextFieldURL.Text;
                            m.ThumbImage = "/Images/audio_x_monkey.png";
                            break;
                    }
                    if (bitEnt.SaveChanges() > 0)
                    {
                        (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa media thành công!");
                        Window1.Hide();
                        ReloadTargetData();
                    }
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnGetVideoInfo_Click(object sender, DirectEventArgs e)
        {
            try
            {
                BitPortal.Utils.VideoInfo video = BitPortal.Utils.VideoUtils.GetYoutubeVideo(txtLink.Text);
                videoUrl.TextFieldURL.Text = video.Url;
                txtTitle.Text = video.Title;
                txtDescription.Text = video.Description;
                txtTag.Text = video.Keyword;
                txtDuration.Text = video.Duration.ToString();
                thumbnailUrl.TextFieldURL.Text = video.Thumbnail;
                thumbnailUrl.TextFieldURL.Focus();
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Thông báo lỗi", "URL nhập vào không hợp lệ <br/> " + ex.Message);
            }
            wYoutube.Hide();
        }

        public void BindData(long? mediaID)
        {
            if (mediaID == null)
            {
                txtTitle.Text = txtDescription.Text = txtTag.Text = imageUrl.TextFieldURL.Text = videoUrl.TextFieldURL.Text
                    = thumbnailUrl.TextFieldURL.Text = flashUrl.TextFieldURL.Text = audioUrl.TextFieldURL.Text = "";
                ckEnabled.Checked = true;
                txtID.Text = txtDuration.Text = "0";
                TabPanel1.ActiveTabIndex = 0;
                btnUpdate.Hidden = true;
                btnInsert.Hidden = false;

                Window1.Show();
                return;
            }

            Models.Media m = Business.MediaBLL.GetByID((long)mediaID);
            if (m != null)
            {
                txtID.Text = m.MediaID.ToString();
                txtTitle.Text = m.Title;
                txtDescription.Text = m.Description;
                txtTag.Text = m.Tags;
                ckEnabled.Checked = m.IsEnabled;
                switch (m.Type)
                {
                    case "Image":
                        TabPanel1.ActiveTabIndex = 0;
                        imageUrl.TextFieldURL.Text = m.MediaURL;
                        break;
                    case "Video":
                        TabPanel1.ActiveTabIndex = 1;
                        videoUrl.TextFieldURL.Text = m.MediaURL;
                        thumbnailUrl.TextFieldURL.Text = m.ThumbImage;
                        txtDuration.Text = m.Duration.ToString();
                        break;
                    case "Flash":
                        TabPanel1.ActiveTabIndex = 2;
                        flashUrl.TextFieldURL.Text = m.MediaURL;
                        break;
                    case "Audio":
                        TabPanel1.ActiveTabIndex = 3;
                        audioUrl.TextFieldURL.Text = m.MediaURL;
                        break;
                }
                btnUpdate.Hidden = false;
                btnInsert.Hidden = true;
                Window1.Show();
            }
        }
    }
}